
/* 
	MakeSpherical tool made by Dave
		
	Thanks to Divran for help busting bugs (and for teaching me how to make tools).
*/

if CLIENT then
	
	language.Add( "Tool_MakeSpherical_name", "Spherical Collisions" )
	language.Add( "Tool_MakeSpherical_desc", "Gives entities a spherical collisions with a defined radius" )
	language.Add( "Tool_MakeSpherical_0", "Left click to make an entity have spherical collisions based on its size. Right click to set collisions with a custom radius" )
	language.Add( "Tool_MakeSpherical_1", "Left click to make an entity have spherical collisions based on its size. Right click to set collisions with a custom radius" )
	language.Add( "MakeSpherical_radius", "Set radius: " )
	
	function TOOL.BuildCPanel( panel )
		
		panel:AddControl("Header", { Text = "#Tool_MakeSpherical_name", Description = "Tool_MakeSpherical_desc" })
		
		panel:AddControl("Slider", {
			Label = "#MakeSpherical_radius",
			Type = "Float",
			Min = "1",
			Max = "1000",
			Command = "makespherical_radius"
		})
	
end
	
end

TOOL.Category		= "Construction"
TOOL.Name			= "#MakeSpherical"
TOOL.ClientConVar[ "radius" ] = "20"

local function IsItOkToFuckWith( this )
	
	if !this || !this:IsValid() then return false end
	if this:GetClass() ~= "prop_physics" && !string.find( this:GetClass(), "gmod_" ) then return false end
	if string.find(this:GetClass(), "wheel") || string.find(this:GetClass(), "thruster") then return false end
	if SERVER and not this:GetPhysicsObject():IsValid() then return false end
	
	return true
	
end

function MakeSphere(player, entity, data )
	
	if not SERVER then return end
	local physic = entity:GetPhysicsObject()
	
	if data.radius > 1 && data.enabled == true then
		
		local radius = data.radius
		entity:PhysicsInitSphere( radius , physic:GetMaterial() )
		entity:SetCollisionBounds( Vector( -radius, -radius, -radius ), Vector( radius, radius, radius ) )
		physic:Wake()
		
	end

	duplicator.StoreEntityModifier( entity, "sphere", data )
	
end

duplicator.RegisterEntityModifier( "sphere", MakeSphere )

function TOOL:LeftClick( trace )

	local ent = trace.Entity
	if !IsItOkToFuckWith( ent ) then return false end
	local obb = ent:OBBMaxs() - ent:OBBMins()
	
	local data = {}
		data.radius = math.max(obb.x, obb.y, obb.z) / 2
		data.enabled = true

	MakeSphere(self:GetOwner(), ent, data )
	return true
	
end

function TOOL:RightClick( trace )

	local ent = trace.Entity
	if !IsItOkToFuckWith( ent ) then return false end
	
	local data = {}
		data.radius = self:GetClientNumber( "radius" )
		data.enabled = true
	
	MakeSphere(self:GetOwner(), ent, data )
	return true
		
end

function TOOL:Reload( trace )

	local ent = trace.Entity
	if !IsItOkToFuckWith( ent ) then return false end
	
	ent:PhysicsInit( SOLID_VPHYSICS )
	ent:SetMoveType( MOVETYPE_VPHYSICS )
	ent:SetSolid( SOLID_VPHYSICS )
	ent:GetPhysicsObject():Wake()
	
	local data = {}
		data.radius = 0
		data.enabled = false
	
	MakeSphere(self:GetOwner(), ent, data )
	return true
	
end
